/*
* 封装 MODBUS 操作
*/
const Utils = require('../lib/util/Utils');
const dcsService = require('./dcsService');
const grabService = require('./grabService');

exports.setHoldingRegister = async (holdingRegisters) => {
    let date = Utils.formatDate((new Date()).getTime());
    let start_time = date + ' 00:00:00';
    let end_time = date + ' 23:59:59';

    try{
        //1、获取当天抓斗累计数据
        let grabData = await grabService.getDataByRange(start_time, end_time);
        console.log('======== grabData ========>');
        console.log(grabData);

        //2、抓斗数据  保存地址为0，在MODBUS里为40001
        let modbus_address = 0;
        holdingRegisters[modbus_address] = grabData.value;
        //console.log('======== modbus_address =====>', modbus_address, "<===== value =====>", item.value);

        //3、获取DCS数据
        let dcsDataList = await dcsService.getDcsData();
        console.log('======== dcsDataList ========>');
        console.log(dcsDataList);

        //4、DCS数据
        for(let item of dcsDataList){
            modbus_address = item.address - 40001;     //modbus地址  数据从地址0开始
            holdingRegisters[modbus_address] = item.value;
            //console.log('======== modbus_address =====>', modbus_address, "<===== value =====>", item.value);
        }
        //console.log('holdingRegisters ========>');
        //console.log(holdingRegisters);
    }
    catch(e){
        console.log('========== getHoldingRegister error =========', e.message);
    }
}
